package com.dxq.test.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.dxq.test.App;
import com.dxq.test.common.AppManager;
import com.dxq.test.R;
import com.dxq.test.adapter.EventAdapter;
import com.dxq.test.db.Event;
import com.dxq.test.db.Label;
import com.dxq.test.db.Pasture;
import com.dxq.test.db.TablesDao;
import com.j256.ormlite.dao.ForeignCollection;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

/**
 * Author: zsh
 * Date: 2023/11/16
 * Describe: 事件列表页面
 */
public class EventActivity extends AppCompatActivity implements View.OnClickListener {

    public TablesDao tablesDao;

    private ImageView ivBack, ivMore;
    private TextView tvTitle;
    private RecyclerView recyclerView;

    private EventAdapter mAdapter;
    private List<Event> recordList = new ArrayList<>();

    private Label selectLabel;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //添加Activity到堆栈
        AppManager.getAppManager().addActivity(this);
        setContentView(R.layout.activity_event);
        selectLabel = (Label) getIntent().getSerializableExtra("label");
        tablesDao = new TablesDao(this);
        initView();
        initAdapter();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //在appManager结束当前activity
        AppManager.getAppManager().finishActivity(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        getData();
    }

    private void initView() {
        ivBack = findViewById(R.id.ivBack);
        ivMore = findViewById(R.id.ivMore);
        tvTitle = findViewById(R.id.tvTitle);
        ivMore.setImageResource(R.drawable.ic_baseline_add_24);
        if (null == selectLabel) {
            ivMore.setVisibility(View.GONE);
            tvTitle.setText("事件列表");
        } else {
            ivMore.setVisibility(View.VISIBLE);
            tvTitle.setText(selectLabel.getName());
        }
        recyclerView = this.findViewById(R.id.recyclerView);

        ivBack.setOnClickListener(this);
        ivMore.setOnClickListener(this);
    }

    private void initAdapter() {
        recyclerView.setLayoutManager(new GridLayoutManager(this, 1));
//        recyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));//添加分割线
        mAdapter = new EventAdapter(R.layout.item_event, recordList);//
        recyclerView.setAdapter(mAdapter);
        mAdapter.setEmptyView(R.layout.layout_empty);

        //item点击事件
        mAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull @NotNull BaseQuickAdapter<?, ?> adapter,
                                    @NonNull @NotNull View view, int position) {
                //ToastUtils.showShort("item事件");
                Intent intent = new Intent(EventActivity.this, EventAddActivity.class);
                Bundle bundle = new Bundle();
                bundle.putSerializable("event", recordList.get(position));
                if (null != selectLabel)
                    bundle.putSerializable("label", selectLabel);
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });

        //item子view点击事件
        mAdapter.setOnItemChildClickListener(new OnItemChildClickListener() {
            @Override
            public void onItemChildClick(@NonNull @NotNull BaseQuickAdapter adapter,
                                         @NonNull @NotNull View view, int position) {
                //ToastUtils.showShort("child添加事件");
            }
        });
    }

    private void getData() {
        recordList.clear();
        if (null != selectLabel) {
            //根据牲畜查询对应事件
            recordList = tablesDao.queryEventForLabel(selectLabel);
        } else {
            //全部牧场事件
            //查询当前所选择牧场
            Pasture pasture = tablesDao.queryPastureById(App.select_pasture_id);
            if (null != pasture) {
                ForeignCollection<Event> events = pasture.getEvents();
                for (Event event : events) {
                    recordList.add(event);
                }
            }
        }
        mAdapter.setList(recordList);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ivBack:
                EventActivity.this.finish();
                break;

            case R.id.ivMore://添加事件
                Intent intent = new Intent(EventActivity.this, EventAddActivity.class);
                Bundle bundle = new Bundle();
                bundle.putSerializable("event", null);
                if (null != selectLabel)
                    bundle.putSerializable("label", selectLabel);
                intent.putExtras(bundle);
                startActivity(intent);
                break;
        }
    }

    @Override
    public void onBackPressed() {
        this.finish();
    }
}
